/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entities.*;
import entities.Domain;
import java.util.*;
import metadata.SystemMetaData;

/**
 *
 * @author Dragan Sahpaski;
 */
public class ISDV1 {

    Domain domain;
    Atribute itName = new Atribute("It_Name");
    Atribute itDesc = new Atribute("It_Desc");
    Atribute iName = new Atribute("I_Name");
    Atribute iDesc = new Atribute("I_Desc");
    Atribute sName = new Atribute("S_Name");
    Atribute sPhone1 = new Atribute("S_Phone1");
    Atribute sPhone2 = new Atribute("S_Phone2");
    Atribute cName = new Atribute("C_Name");
    Atribute coName = new Atribute("Co_Name");
    Atribute coCode = new Atribute("Co_Code");
    Atribute coCont = new Atribute("Co_Cont");
    Atribute dDate = new Atribute("D_Date");
    Atribute dWeek = new Atribute("D_Week");
    Atribute dMonth = new Atribute("D_Month");
    Atribute dYear = new Atribute("D_Year");
    private static final int BLOCK_SIZE_BYTES = 200;
    private static final int HYBRID_HASH_JOIN_BUFFER_SIZE_BYTES = 100000000;

    public ISDV1() throws Exception {
        Table itemType = new Relation("ItemType", 100, new PrimaryKey(new Atribute("It_Id")),
                new ArrayList<Atribute>(Arrays.asList(itName, itDesc)));
        Table item = new Relation("Item", 10000, new PrimaryKey(new Atribute("I_Id")),
                new ArrayList<Atribute>(Arrays.asList(iName, iDesc)));

        Table country = new Relation("Country", 100, new PrimaryKey(new Atribute("Co_Id")),
                new ArrayList<Atribute>(Arrays.asList(coName, coCode, coCont)));
        Table city = new Relation("City", 1000, new PrimaryKey(new Atribute("C_Id")),
                new ArrayList<Atribute>(Arrays.asList(cName)));
        Table supplier = new Relation("Supplier", 10000, new PrimaryKey(new Atribute("S_Id")),
                new ArrayList<Atribute>(Arrays.asList(sName, sPhone1, sPhone2)));

        Table year = new Relation("Year", 4, new PrimaryKey(new Atribute("Y_Id")),
                new ArrayList<Atribute>(Arrays.asList(dYear)));
        Table month = new Relation("Month", 48, new PrimaryKey(new Atribute("M_Id")),
                new ArrayList<Atribute>(Arrays.asList(dMonth)));
        Table week = new Relation("Week", 208, new PrimaryKey(new Atribute("W_Id")),
                new ArrayList<Atribute>(Arrays.asList(dWeek)));
        Table date = new Relation("Date", 1460, new PrimaryKey(new Atribute("D_Id")),
                new ArrayList<Atribute>(Arrays.asList(dDate)));


        Relation.addRelationship(itemType, item);
        Relation.addRelationship(Relation.getNullRelation(), itemType);
        Relation.addRelationship(Relation.getNullRelation(), country);
        Relation.addRelationship(country, city);
        Relation.addRelationship(city, supplier);
        Relation.addRelationship(Relation.getNullRelation(), year);
        Relation.addRelationship(year, month);
        Relation.addRelationship(month, date);
        Relation.addRelationship(week, date);

        ViewGene root = new ViewGene("ISD", Arrays.asList(item, supplier, date));
        ViewGraph vg = new ViewGraph(root);
        vg.buildViewGraph();
        // System.out.println(vg);
        domain = new Domain(vg, new SystemMetaData(BLOCK_SIZE_BYTES, HYBRID_HASH_JOIN_BUFFER_SIZE_BYTES));
        //System.out.println(Printing.printViews(domain));
        //System.out.println(Printing.printRelations(domain));
    }

    public Domain getDomain() {
        return domain;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    public List<Query> getTrivialQueries() {
        Query query1 = new Query(domain,
                new ArrayList<Condition>(
                Arrays.asList(new Condition(itName, Condition.Predicate.Equal),
                new Condition(iName, Condition.Predicate.Equal))),
                new ArrayList<Atribute>(Arrays.asList(itName, iName)));
        Query query2 = new Query(domain,
                new ArrayList<Condition>(
                Arrays.asList(new Condition(sName, Condition.Predicate.Equal),
                new Condition(coName, Condition.Predicate.Equal))),
                new ArrayList<Atribute>(Arrays.asList(sName, coName)));
        Query query3 = new Query(domain,
                new ArrayList<Condition>(
                Arrays.asList(new Condition(dYear, Condition.Predicate.Equal),
                new Condition(dWeek, Condition.Predicate.Equal))),
                new ArrayList<Atribute>(Arrays.asList(dYear, dWeek)));

        List<Query> queries = new ArrayList<Query>();
        queries.add(query1);
        queries.add(query2);
        queries.add(query3);
        return queries;
    }

    public static void main(String[] args) throws Exception {
        new ISDV1();
    }
}
